package com.es.system.controller;


import com.es.api.PageModel;
import com.es.api.SystemResult;
import com.es.system.domain.SysUser;
import com.es.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.es.core.controller.BaseController;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author everysucceed
 * @since 2021-12-09
 */
@Controller
@RequestMapping("/system/user/")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService service;

    @GetMapping("/manage")
    public String manage(){
        return "system/user/manage";
    }

    @GetMapping("/list")
    @ResponseBody
    public SystemResult<List<SysUser>> list(PageModel<SysUser> pageModel){
        if(null == pageModel.getPage()){
            pageModel.setPage(PageModel.DEFAULT_PAGE);
        }
        if(null == pageModel.getLimit()){
           pageModel.setLimit(PageModel.DEFAULT_LIMIT);
        }
        SystemResult<List<SysUser>> result =service.page(pageModel.getPage(),pageModel.getLimit(),pageModel.getModel());

        return result;
    }

    @GetMapping("/add")
    public String addPage(){
        return "system/user/add";
    }

    @PostMapping("/save")
    @ResponseBody
    public SystemResult<Serializable> save(@RequestBody SysUser sysUser){
        return service.save(sysUser);
    }

    @DeleteMapping("/remove/{dataId}")
    @ResponseBody
    public SystemResult<String> remove(@PathVariable("dataId") String dataId){
        boolean flag =  service.removeById(Long.valueOf(dataId));
        if(flag){
            return SystemResult.success("删除成功");
        }else{
            return SystemResult.fail("删除失败");
        }
    }

}
